package com.atwu.wl2022.config;

import com.atwu.wl2022.entity.Article;
import com.atwu.wl2022.service.IArticleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * @author wulejay
 */
@Component
@Slf4j
public class Jobs {

	@Autowired
	IArticleService iArticleService;

	@Autowired
	RedisTemplate<String, Object> redisTemplate;

	/**
	 * 表示每天3时30分0秒执行
	 */
	@Scheduled(cron = "0 0 8/2 * * ?")
	public void cronJob() {
		log.info("定时任务开启：{}",new Date());
		List<Article> articleList = iArticleService.list(new QueryWrapper<Article>().select("id"));
		for (Article article:
				articleList) {
			String  key= "POST_ARTICLE:"+article.getId();
			System.out.print("key====>"+key+"----");
			Integer keyValue = (Integer) redisTemplate.boundHashOps("HashKey").get("POST_ARTICLE:"+article.getId());
			System.out.println("keyValue====>"+keyValue);
			if(keyValue!=null){
				Article byId = iArticleService.getById(article.getId());
				byId.setLikes(byId.getLikes()+keyValue);
				boolean update = iArticleService.updateById(byId);
				System.out.println(update);
			}
		}
		redisTemplate.delete("HashKey");
	}
}



